package com.example.module_main.dialog

import android.os.Bundle
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.example.module_main.R
import com.example.module_main.databinding.ActivityGuideBinding
import com.example.module_main.databinding.DialogAppUpdateBinding
import com.example.module_main.dialog.MessageNoticeDialog.Companion

/**
 * @author yaotianxue
 * @description 版本升级对话框
 * @date 2025/1/17
 */
class AppUpdateDialog(
    private val title:String? = null,
    private val message:String? = null,
    private val force:Boolean = false, //强制更新：true 没有X
    private val onUpdateClick:()->Unit
): DialogFragment(){
    private lateinit var mParentView: View
    private lateinit var mBinding: DialogAppUpdateBinding
    companion object{
        val TAG = AppUpdateDialog::class.java.simpleName
    }
    //创建
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.MessageNoticeDialog)
    }
    /**
     * 调整对话框的宽高和位置
     */
    override fun onStart() {
        super.onStart()
        //获得对话框的属性
        val window = dialog?.window
        val attributes = window?.attributes
        //获得窗口的宽度和高度
        val dm = DisplayMetrics()
        activity?.windowManager?.defaultDisplay?.getMetrics(dm)
        window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
        //修改对话框的属性并重新设置给当前对话框
        attributes?.width = (dm.widthPixels*0.7).toInt()
        attributes?.gravity = Gravity.CENTER
        window?.attributes = attributes
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mBinding = DataBindingUtil.inflate(inflater,R.layout.dialog_app_update,container,false)
        mParentView = mBinding.root
        return mParentView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        dialog?.setCancelable(false)
        dialog?.setCanceledOnTouchOutside(false)

        mBinding.tvMessage.text = message
        mBinding.tvTitle.text = title
        //强制更新
        if(force){
            mBinding.tvCancel.visibility = View.GONE
        }else{
            mBinding.tvCancel.visibility = View.VISIBLE
            mBinding.tvCancel.setOnClickListener{
                dismiss()
            }
        }

        mBinding.btnUpdate.setOnClickListener {
            onUpdateClick?.invoke()
            dismiss()
        }
    }

    /**
     * 打开对话框
     */
    fun show(fragmentManager: FragmentManager){
        if(fragmentManager.findFragmentByTag(MessageNoticeDialog.TAG)?.isVisible === true){
            return
        }
        this.show(fragmentManager, MessageNoticeDialog.TAG)
    }
}